#include "recognizeoperation.h"
#include <QDebug>

RecognizeOperation::RecognizeOperation(QObject *parent)
    : Operation{parent}
    , m_nImageNum(1)
    , m_pDescription((Description*)parent){

}

Operation* RecognizeOperation::clone(Operation* pOperation){
    Operation::clone(pOperation);

    m_nImageNum = ((RecognizeOperation*)pOperation)->GetImageNum();
    m_pDescription = ((RecognizeOperation*)pOperation)->GetDescription();
    return this;
}

int RecognizeOperation::nImageNum() const
{
    return m_nImageNum;
}

void RecognizeOperation::setNImageNum(int newNImageNum)
{
    m_nImageNum = newNImageNum;
}

void RecognizeOperation::run(){
    Device* pDevice = m_pDescription->FindDeviceById(m_strDeviceID);
    QMap<QString,QString> map;
    map.insert("classname",this->getClassName());
    map.insert("content",QString::number(m_nImageNum));
    pDevice->writeData(QVariant::fromValue(map));
}

QString RecognizeOperation::getClassName(){
    return this->metaObject()->className();
}

void RecognizeOperation::writeXML(QXmlStreamWriter& writer){
    // 写入当前操作的信息
    writer.writeStartElement("RecognizeOperation");
    writer.writeAttribute("device", m_strDeviceID);

    writer.writeTextElement("image", QString::number(m_nImageNum));

    writer.writeEndElement();  // 结束当前操作的元素
}

void RecognizeOperation::readFromXML(QXmlStreamReader& reader){
    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("device")) {
        m_strDeviceID = attributes.value("device").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("RecognizeOperation"))) {

        if (reader.name() == QStringLiteral("image")) {
            m_nImageNum = reader.readElementText().toInt();
        }

        reader.readNext();
    }
}
